/*********************************************************************************************************
Class Name: NMRDoNotCall
 * Author: Accenture
 * Date: 29-March-2012 
 * Requirement/Project Name: NMR
 * Requirement Description: Class to handle Do Not Call Functionality on Lead and Do No Call Object.
 * Update - Test class added to improve coverage - 30th March 2012.
********************************************************************************************************/


public class NMRDoNotCall{
    //Record Type ID's for Lead and Do_Not_call__c
    public static ID dncRecordTypeId = RecordTypeHelper.GetRecordTypeID('Do_Not_Call__c','Do Not Call');
    public static ID leadRecordTypeId = RecordTypeHelper.GetRecordTypeID('Lead','NMR Lead');
    public static Id nmrLeadCustIdRecordTypeID = RecordTypeHelper.GetRecordTypeID('Lead', 'NMR Lead with CustomerId');
    //public static ID dncRecordTypeId = [SELECT Id, Name, SObjectType FROM RecordType WHERE SobjectType = 'Do_Not_Call__c' AND Name = 'Do Not Call' limit 1].Id;
    //public static ID leadRecordTypeId = [SELECT Id, Name, SObjectType FROM RecordType WHERE SobjectType = 'Lead' AND Name = 'NMR Lead' limit 1].Id;
    //Code to handle operations on Do_Not_Call__c object
    public static void nmrDoNotCallLeadUpdate(Set<String> phoneSet){
        list<Lead> leadList = new list<Lead>(); //lead list that matches phone number in DNC.                                             
        if(phoneSet.size()>0){
            leadList = [Select Id,Phone,NMR_Additional_Phone_1__c,NMR_Additional_Phone_2__c,NMR_Additional_Phone_3__c,NMR_Additional_Phone_4__c,NMR_Additional_Phone_5__c,DoNotCall,Do_Not_Call_1__c,Do_Not_Call_2__c,Do_Not_Call_3__c,Do_Not_Call_4__c,Do_Not_Call_5__c,Status,Bad_Reason__c from Lead where (RecordTypeId =:leadRecordTypeId OR RecordTypeId =:nmrLeadCustIdRecordTypeID) AND (Phone in: phoneSet OR NMR_Additional_Phone_1__c in: phoneSet OR NMR_Additional_Phone_2__c in: phoneSet OR NMR_Additional_Phone_3__c in: phoneSet OR NMR_Additional_Phone_4__c in: phoneSet OR NMR_Additional_Phone_5__c in:phoneSet) AND Id NOT IN: NMRUtil.processed AND IsConverted != True];
        }        
        for(Lead SingleLead: leadList){
        if(phoneSet.contains(SingleLead.Phone) && SingleLead.DoNotCall == False){
        // Updating status of the Lead to invalid on Primary Phone being marked DNC.
                SingleLead.DoNotCall = True;
                SingleLead.Status = 'Invalid';
                SingleLead.Bad_Reason__c = 'Do Not Call';

            }
            //Code to cover additional phone numbers on a Lead
            else{
                SingleLead.DoNotCall = False;
            }
            if(phoneSet.contains(SingleLead.NMR_Additional_Phone_1__c) && SingleLead.Do_Not_Call_1__c== False){
                SingleLead.Do_Not_Call_1__c = True;    
            }
            else{
                SingleLead.Do_Not_Call_1__c = False;
            }
            if(phoneSet.contains(SingleLead.NMR_Additional_Phone_2__c) && SingleLead.Do_Not_Call_2__c== False){
                SingleLead.Do_Not_Call_2__c = True;    
            }
            else{
                SingleLead.Do_Not_Call_2__c = False;
            }
            if(phoneSet.contains(SingleLead.NMR_Additional_Phone_3__c) && SingleLead.Do_Not_Call_3__c== False){
                SingleLead.Do_Not_Call_3__c = True;    
            }
            else{
                SingleLead.Do_Not_Call_3__c = False;
            }
            if(phoneSet.contains(SingleLead.NMR_Additional_Phone_4__c) && SingleLead.Do_Not_Call_4__c== False){
                SingleLead.Do_Not_Call_4__c = True;    
            }
            else{
                SingleLead.Do_Not_Call_4__c = False;
            }
            if(phoneSet.contains(SingleLead.NMR_Additional_Phone_5__c) && SingleLead.Do_Not_Call_5__c== False){
                SingleLead.Do_Not_Call_5__c = True;    
            }
            else{
                SingleLead.Do_Not_Call_5__c = False;
            }            
        }
        if(leadList.size()>0){
            Update leadList;
        }   

    }
    
    public static void nmrLeadDoNotCallCreateUpdate(Map<String, Lead> leadMap,Set<String> phoneSet,Set<String> primaryPhoneSet,List<Lead> LeadDNC){
        list<Do_Not_Call__c> DNC = new list<Do_Not_Call__c>(); //List of DNC that matches Lead phone numbers
        List<Do_Not_Call__c> dncUpdated = new List<Do_Not_Call__c>(); // List of DNC's that need to be updated
        List<Do_Not_Call__c> insertDNC = new List<Do_Not_Call__c>(); //List of DNC records that need to be inserted into system.
        Set<String>  dncPhoneSet = new Set<String>(); 
        if(leadDNC.size()>0){
            DNC = [Select Id,Active__c,Phone_Number__c from Do_Not_Call__c where RecordTypeId =:dncRecordTypeId AND Phone_Number__c in: phoneSet AND Id NOT IN: NMRUtil.processed ];                        
        }              
            
            // Code for updating the Do Not Call Record, as well as updating the status and Bad reason field values on the Leads.
            for( Do_Not_Call__c DoNotCall: DNC ){                              
                if( DoNotCall.Active__c==False ){                   
                    DoNotCall.Active__c=True;
                    if( primaryPhoneSet.contains(DoNotCall.Phone_Number__c )){
                        if( leadMap.containsKey(DoNotCall.Phone_Number__c )){
                             leadMap.get( DoNotCall.Phone_Number__c ).Status = 'Invalid';                
                             leadMap.get( DoNotCall.Phone_Number__c ).Bad_Reason__c = 'Do Not Call';
                        }
                   } 
                }
                else{                   
                    DoNotCall.Active__c=False;
                }
                dncUpdated.add( DoNotCall );                      
            }    
            Update dncUpdated;    
            
             for(Do_Not_Call__c dncPhonelist1: [Select Phone_Number__c from Do_Not_Call__c where RecordTypeId =:dncRecordTypeId ]){
                        dncPhoneSet.add(dncPhonelist1.Phone_Number__c);                                      
                 }
             for(String singlePhone: phoneSet){
                 if(!dncPhoneSet.contains(singlePhone)){
                     Do_Not_Call__c newDNC = new Do_Not_Call__c();
                     newDNC.Phone_Number__c = singlePhone;
                     newDNC.Active__c = True;
                     //newDNC.Submitted__c = system.today();
                     newDNC.RecordTypeId = dncRecordTypeId; 
                     insertDNC.add(newDNC);
                 }                 
             }    
             insert insertDNC;                
    }
    @isTest(SeeAllData=True)
    static void dncTest() {
    /**
          list<Lead> testLead = new List<Lead>();
          list<Do_Not_Call__c> dncList = new List<Do_Not_Call__c>();
          Lead dncLead = new Lead();
          dncLead.FirstName = 'TestLead';
          dncLead.LastName = 'TestLead';
          dncLead.Company = 'TestCompany';
          dncLead.Phone = '9558499446';  
          dncLead.NMR_Additional_Phone_1__c = '9558499447';
          dncLead.NMR_Additional_Phone_2__c = '9558499448';
          dncLead.NMR_Additional_Phone_3__c = '9558499449';
          dncLead.NMR_Additional_Phone_4__c = '9558499450';
          dncLead.NMR_Additional_Phone_4__c = '9558499451';
          dncLead.RecordTypeId = leadRecordTypeId;  
          
          Lead dncLead1 = new Lead();
          dncLead1.FirstName = 'TestLead1';
          dncLead1.LastName = 'TestLead1';
          dncLead1.Company = 'TestCompany1';
          dncLead1.Status = 'New';
          dncLead1.Bad_Reason__c = '';
          dncLead1.Phone = '5598499446';  
          dncLead1.NMR_Additional_Phone_1__c = '5598499447';
          dncLead1.NMR_Additional_Phone_2__c = '5598499448';
          dncLead1.NMR_Additional_Phone_3__c = '5598499449';
          dncLead1.NMR_Additional_Phone_4__c = '5598499450';
          dncLead1.NMR_Additional_Phone_4__c = '5598499451';
          dncLead1.RecordTypeId = leadRecordTypeId;  
          testLead.add(dncLead);
          testLead.add(dncLead1);
          
          Do_Not_Call__c dnc1 = new Do_Not_call__c();
          dnc1.Phone_Number__c = '5598499446';
          dnc1.RecordTypeId = dncRecordTypeId;
          
          Do_Not_Call__c dnc2 = new Do_Not_call__c();
          dnc2.Phone_Number__c = '5598499447';
          dnc2.RecordTypeId = dncRecordTypeId;
          
          Do_Not_Call__c dnc3 = new Do_Not_call__c();
          dnc3.Phone_Number__c = '5598499448';
          dnc3.RecordTypeId = dncRecordTypeId;
          
          Do_Not_Call__c dnc4 = new Do_Not_call__c();
          dnc4.Phone_Number__c = '5598499449';
          dnc4.RecordTypeId = dncRecordTypeId;
          
          Do_Not_Call__c dnc5 = new Do_Not_call__c();
          dnc5.Phone_Number__c = '5598499450';
          dnc5.RecordTypeId = dncRecordTypeId;
          
          Do_Not_Call__c dnc6 = new Do_Not_call__c();
          dnc6.Phone_Number__c = '5598499451';
          dnc6.RecordTypeId = dncRecordTypeId;
          
          dncList.add(dnc1);
          dncList.add(dnc2);
          dncList.add(dnc3);
          dncList.add(dnc4);
          dncList.add(dnc5);
          dncList.add(dnc6);
          test.startTest();
          insert testLead;
          insert dncList;
          
          dncList[0].Active__c = True;
          dncList[1].Active__c = True;
          dncList[2].Active__c = True;
          dncList[3].Active__c = True;
          dncList[4].Active__c = True;
          dncList[5].Active__c = True;
          update dncList;           
                    
          dncLead.DoNotCall = True;
          dncLead.Do_Not_Call_1__c = True;
          dncLead.Do_Not_Call_2__c = True;
          dncLead.Do_Not_Call_3__c = True;
          dncLead.Do_Not_Call_4__c = True;
          dncLead.Do_Not_Call_5__c = True;
          update dncLead;  
          
          dncLead.DoNotCall = True;
          dncLead.Do_Not_Call_4__c = False;
          dncLead.Do_Not_Call_5__c = False;
          update dncLead; 
          delete dnc1;         
        test.stopTest();**/
    }
       static testMethod void userTest(){
       //Do not remove comment as it causes Mixed DML exception    
     /*Sales_Office__c sOffice = new  Sales_Office__c ();
      sOffice.Office_Code__c = 'NYC';
      sOffice.Office_Name__c = 'NYC';
      sOffice.Time_Zone__c = 'America/Los_Angeles';
      insert sOffice;*/
      
     Id masterrole  = [select id from userRole where name =:'Information Systems'].id;
     profile profid = [Select Id from profile q where profile.Name ='NMR Sales']; 
     List<UserRole> RoleList = new List<UserRole>();  
     UserRole singlerole = new UserRole();
     singlerole.Name = 'NMR DUMMY';
     singlerole.ParentRoleId=masterrole;   
     RoleList.add(singlerole);  
     insert singlerole;
     
     Test.StartTest();
     List<User> user = new List<User>();  

     User user1 = new User(alias = 'Opt21', email='sysadminOpty21@accenture.com',emailencodingkey='UTF-8', lastname='Test Opty 21', languagelocalekey='en_US',localesidkey='en_US', profileid = profid.id,timezonesidkey='America/Los_Angeles', username='testOppty21@testorg.com',SFA_Office__c='NYC',IsActive = True,css_office_name__c = 'NYC',userroleid= singlerole.id);              
     User user2 = new User(alias = 'Opt22', email='sysadminOpty22@accenture.com',emailencodingkey='UTF-8', lastname='Test Opty 22', languagelocalekey='en_US',localesidkey='en_US', profileid = profid.id,timezonesidkey='America/Los_Angeles', username='testOppty22@testorg.com',SFA_Office__c='NYC',IsActive = True,css_office_name__c = 'NYC',userroleid= singlerole.id);              
      user.add(user1);
      user.add(user2);
      insert user;
     Test.StopTest();
     
    }      
}